package com.keta.foundation.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.keta.foundation.dao.UserRoleDAO;
import com.keta.foundation.entity.Role;
import com.keta.foundation.entity.User;
import com.keta.foundation.entity.UserRole;
import com.keta.foundation.service.UserRoleService;

@Service
@Transactional
public class UserRoleServiceImpl implements UserRoleService {

	@Autowired
	private UserRoleDAO userRoleDAO;

	@Override
	public void save(UserRole userRole) {
		userRoleDAO.save(userRole);
	}

	@Override
	public void delete(UserRole userRole) {
		userRoleDAO.delete(userRole);
	}

	@Override
	public List<UserRole> getUserRole(User user) {

		return userRoleDAO.findByUserId(user.getId());
	}

	@Override
	public UserRole get(Long id) {
		return userRoleDAO.findOne(id);
	}

	@Override
	public void delByRole(Long roleId) {
		List<UserRole> listUserRole = userRoleDAO.findByRoleId(roleId);
		for (UserRole userRole : listUserRole) {
			userRoleDAO.delete(userRole);
		}
	}

	@Override
	public void delByUser(Long userId) {
		List<UserRole> listUserRole = userRoleDAO.findByUserId(userId);
		for (UserRole userRole : listUserRole) {
			userRoleDAO.delete(userRole);
		}
	}

	@Override
	public void delByRole(Role role) {
		List<UserRole> listUserRole = userRoleDAO.findByRoleId(role.getId());
		for (UserRole userRole : listUserRole) {
			userRoleDAO.delete(userRole);
		}
	}

	@Override
	public void delByUser(User user) {
		List<UserRole> listUserRole = userRoleDAO.findByUserId(user.getId());
		for (UserRole userRole : listUserRole) {
			userRoleDAO.delete(userRole);
		}
	}
}
